package com.leadership.api.service.impl;

import java.util.List;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.leadership.training.entity.project.ProjectPhaseCourse;
import com.leadership.training.entity.report.TrainerCourseReport;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo2do.core.persistence.annotation.Dao;
import com.leadership.api.dao.TrainingSurveyResultDao;
import com.leadership.api.service.TrainingSurveyResultService;
import com.leadership.training.entity.project.TrainingSurveyResult;

/**
 * 
 * @author Rockett
 *
 */
@Service("trainingSurveyResultService")
public class TrainingSurveyResultServiceImpl extends DataServiceImpl<TrainingSurveyResult> implements TrainingSurveyResultService{

	private static final Logger logger = LoggerFactory.getLogger(TrainingSurveyResultService.class);

	@Dao
    private TrainingSurveyResultDao trainingSurveyResultDao;

	@Autowired
	private GenericDaoSupport genericDaoSupport;

	/*
	 * (non-Javadoc)
	 * @see com.training.admin.service.TrainingSurveyResultService#listByTraineeAndProjectPhase(java.lang.Long, java.lang.Long)
	 */
	public List<TrainingSurveyResult> listByTraineeAndProjectPhase(Long traineeId,Long projectPhaseId) {
		return trainingSurveyResultDao.listByTraineeAndProjectPhase(traineeId,projectPhaseId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.training.admin.service.TrainingSurveyResultService#listByTraineeAndProjectPhaseAndCourse(java.lang.Long, java.lang.Long, java.lang.Long)
	 */
	public List<TrainingSurveyResult> listByTraineeAndProjectPhaseAndCourse(Long traineeId,Long projectPhaseId,Long projectPhaseCourseId) {
		return trainingSurveyResultDao.listByTraineeAndProjectPhaseAndCourse(traineeId,projectPhaseId,projectPhaseCourseId);
	}

	
	
	/*
	 * (non-Javadoc)
	 * @see com.training.admin.service.TrainingSurveyResultService#listByProjectPhase(java.lang.Long)
	 */
	public List<TrainingSurveyResult> listByProjectPhase(Long projectPhaseId) {
		return trainingSurveyResultDao.listByProjectPhase(projectPhaseId);
	}
    

	/*
	 * (non-Javadoc)
	 * @see com.training.admin.service.TrainingSurveyResultService#listByCourse(java.lang.Long)
	 */
	public List<TrainingSurveyResult> listByCourse(Long courseId) {
		return trainingSurveyResultDao.listByCourse(courseId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.training.admin.service.TrainingSurveyResultService#listByProjectPhaseCourse(java.lang.Long)
	 */
	public List<TrainingSurveyResult> listByProjectPhaseCourse(Long projectPhaseCourseId) {
		return trainingSurveyResultDao.listByProjectPhaseCourse(projectPhaseCourseId);
	}
	
	/* (non-Javadoc)
	 * @see com.training.admin.service.TrainingSurveyResultService#countByProject(java.lang.Long)
	 */
	public int countByProject(Long projectId) {
		return trainingSurveyResultDao.countByProject(projectId);
	}

	/* (non-Javadoc)
	 * @see com.training.admin.service.TrainingSurveyResultService#avgByTrainer(java.lang.Long)
	 */
	public Double avgByTrainer(Long trainerId) {
		return trainingSurveyResultDao.avgByTrainer(trainerId);
	}

	/* (non-Javadoc)
	 * @see com.training.admin.service.TrainingSurveyResultService#listScoreByTrainer(java.lang.Long)
	 */
	public List<TrainingSurveyResult> listScoreByTrainer(Long trainerId) {
		return trainingSurveyResultDao.listScoreByTrainer(trainerId);
	}

	/**
	 * @param trainerCourseReportList
	 * @return void
	 * @description
	 * @params * @param trainerCourseReportList
	 * @author LZJ
	 * @date 2019/12/13 12:56 *
	 */
	public void resultDataUpdate(List<TrainerCourseReport> trainerCourseReportList) {

		try {

			if(org.apache.commons.collections.CollectionUtils.isNotEmpty(trainerCourseReportList)) {
				for(TrainerCourseReport trainerCourseReportItem : trainerCourseReportList) {

					Long projectPhaseCourseId = trainerCourseReportItem.getProjectPhaseCourseId();
					ProjectPhaseCourse phaseCourse = genericDaoSupport.load(ProjectPhaseCourse.class, projectPhaseCourseId);

					String oldPiPeiDu = phaseCourse.getPiPeiDu();
					String oldZsjq = phaseCourse.getZsjq();
					String oldLjsd = phaseCourse.getLjsd();
					String oldSurveySuggestion = phaseCourse.getSurveySuggestion();
					String oldCourseSurveyResult = phaseCourse.getCourseSurveyResult();
					String oldTrainerSurveyResult = phaseCourse.getTrainerSurveyResult();

					String piPeiDu = trainerCourseReportItem.getPiPeiDu();
					String zsjq = trainerCourseReportItem.getZsjq();
					String ljsd = trainerCourseReportItem.getLjsd();
					String surveySuggestion = trainerCourseReportItem.getGjjy();
					String courseSurveyResult = trainerCourseReportItem.getCourseSurveyResult();
					String trainerSurveyResult = trainerCourseReportItem.getTrainerSurveyResult();



					//如果数据存在，比对4个字段是否更新，并且更新数据到最新
					if(piPeiDu !=null && StringUtils.isNotBlank(piPeiDu)){
						if(oldPiPeiDu == null || !piPeiDu.equals(oldPiPeiDu) ){
							phaseCourse.updatePiPeiDu(piPeiDu);
						}
					}

					if(zsjq != null && StringUtils.isNotBlank(zsjq)){
						if(oldZsjq == null || !zsjq.equals(oldZsjq)){
							phaseCourse.updateZsjq(zsjq);
						}
					}

					if(ljsd != null && StringUtils.isNotBlank(ljsd)){
						if(oldLjsd == null || !ljsd.equals(oldLjsd)){
							phaseCourse.updateLjsd(ljsd);
						}
					}

					if(surveySuggestion != null && StringUtils.isNotBlank(surveySuggestion)){
						if(oldSurveySuggestion == null || !surveySuggestion.equals(oldSurveySuggestion)){
							phaseCourse.updateSurveySuggestion(surveySuggestion);
						}
					}

					if(courseSurveyResult != null && StringUtils.isNotBlank(courseSurveyResult)){
						if(oldCourseSurveyResult == null || !courseSurveyResult.equals(oldCourseSurveyResult)){
							phaseCourse.updateCourseSurveyResult(courseSurveyResult);
						}
					}

					if(trainerSurveyResult != null && StringUtils.isNotBlank(trainerSurveyResult)){
						if(oldTrainerSurveyResult == null || !trainerSurveyResult.equals(oldTrainerSurveyResult)){
							phaseCourse.updateTrainerSurveyResult(trainerSurveyResult);
						}
					}

					genericDaoSupport.update(phaseCourse);
				}
			}

		}catch (Exception e){

			logger.error("#resultDataUpdate exception [{}]",e);
		}

	}
}
